package com.baosight.risk.web.xtzy;

import java.util.List;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baosight.risk.api.xtzy.IXtzyUserService;
import com.baosight.risk.base.controller.BaseController;
import com.baosight.risk.common.annotation.Log;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.enums.BusinessType;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.service.quartz.entity.SyncEmpleeEntitiy;
import com.baosight.risk.service.xtzy.entity.XtzyUser;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 类 名: XtzyUserController
 * 描 述: 用户管理
 * 作 者: 黎秋荣
 * 创 建：2019年04月25日
 * 版 本：v2.2.0
 * 历 史: (版本) 作者 时间 注释
 */
@Slf4j
@RestController
@RequestMapping("/xtzy/user")

@Api(value = "用户管理", description = "用户管理", produces = MediaType.APPLICATION_JSON)
public class XtzyUserController extends BaseController {

    @Autowired
    private IXtzyUserService userService;

    /**
     * 根据ID查询
     *
     * @return
     */
    @GetMapping("auth")
    @ApiOperation("根据ID查询")
    public AjaxResult auth(@RequestParam("id") String id) {
        return success(userService.get(id));
    }

    /**
     * 分页查询数据
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    @ApiOperation("根据ID查询")
    public AjaxResult list(XtzyUser user) {
        startPage();
        return getResultPage(userService.selectUser(user));
    }

    /**
     * 保存用户
     *
     * @param user
     * @return
     */
    @PostMapping("save")
    @ApiOperation("保存用户")
    public AjaxResult save(@RequestBody XtzyUser user) {
        log.info("用户添加参数：" + user.toString());
        //初始化基本参数
        user = (XtzyUser) BeanUtils.initBean(user);
        if (StringUtils.isEmpty(user.getUserCode())) {
            return error("登录账号不能为空");
        }
        if (StringUtils.isEmpty(user.getUserDesc())) {
            return error("用户名称不能为空");
        }
        //默认状态禁用
        user.setStatus("20");
        Integer result = userService.insertUser(user);
        return toAjax(result);
    }

    /**
     * 进入修改页面
     *
     * @return
     */
    @GetMapping("edit")
    @ApiOperation("根据ID查询")
    public AjaxResult edit(@RequestParam("id") String id) {
        return success(userService.get(id));
    }

    /**
     * 修改数据
     *
     * @param user
     * @return
     */
    @PostMapping("update")
    @Log(title = "修改用户", businessType = BusinessType.UPDATE)
    @RequiresPermissions("xtzy:user:update")
    @ApiOperation("修改用户")
    public AjaxResult update(@RequestBody XtzyUser user) {
        log.info("修改用户：" + user.toString());
        user = (XtzyUser) BeanUtils.updateBean(user);
        userService.updateExpireDate();
        return toAjax(userService.update(user));
    }


    @PostMapping("delete")
    @Log(title = "禁用用户", businessType = BusinessType.DELETE)
    @RequiresPermissions("xtzy:user:delete")
    @ApiOperation("禁用用户")
    public AjaxResult delete(@RequestBody List<String> ids) throws Exception {
//        List<String> idss= JSONArray.fromObject(ids);
        String status = "20";
        return userService.deleteById(ids, status);
    }

    @PostMapping("open")
    @Log(title = "开启用户", businessType = BusinessType.DELETE)
    @RequiresPermissions("xtzy:user:delete")
    @ApiOperation("开启用户")
    public AjaxResult open(@RequestBody List<String> ids) throws Exception {
//        List<String> idss= JSONArray.fromObject(ids);
        String status = "10";
        return userService.openById(ids, status);
    }

    /**
     * 查询外部数据库人员信息（全部数据）
     *
     * @param syncEmpleeEntitiy
     * @return
     */
    @GetMapping("getEmpleeFromOtherDB")
    @ApiOperation("查询外部数据库人员信息（全部数据）")
    public AjaxResult getEmpleeFromOtherDB(SyncEmpleeEntitiy syncEmpleeEntitiy) {
        log.info("查询外部数据库人员信息：" + syncEmpleeEntitiy.toString());
        startPage();
        List<SyncEmpleeEntitiy> result = userService.getEmpleeFromOtherDB(syncEmpleeEntitiy);
        return getResultPage(result);
    }
    
    /**
     * 修改数据
     * @param userCode
     * @param oldPwd
     * @param newPwd
     * @return
     */
    @PostMapping("changePassWord")
    @Log(title = "修改密码", businessType = BusinessType.UPDATE)
    @RequiresPermissions("xtzy:user:changePassWord")
    @ApiOperation("修改密码")
    public AjaxResult changePassWord(@RequestBody Map<String, String> map) {
        return toAjax(userService.changePwd(this.getUserName(),(String)map.get("oldPwd"),(String)map.get("newPwd")));
    }
    
    
    @PostMapping("deleteOA")
    @Log(title = "删除OA4.0待办", businessType = BusinessType.DELETE)
    @RequiresPermissions("xtzy:user:deleteOA")
    @ApiOperation("删除OA4.0待办")
    public AjaxResult deleteOA(@RequestBody List<String> ids) {
    	try {
			userService.deleteOA(ids);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return error();
		}
        return success();
    }
}
